﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FriendlyURL.Web.UI.Helpers;
using FriendlyURL.Web.Domain.Abstract;
using FriendlyURL.Web.UI.Models;

namespace FriendlyURL.Web.UI.Controllers
{
    //[CustomAuthorize(UserRole.Owner, UserRole.Administrator)]
    public class HomeController : Controller
    {
        private IConfiguration configuration;
        private ILinkRepository linkRepository;
        private IUserRepository userRepository;
        private IMessageHandler messageHandler;

        public HomeController(IConfiguration configuration, ILinkRepository linkRepository, IUserRepository userRepository, IMessageHandler messageHandler)
        {
            this.configuration = configuration;
            this.linkRepository = linkRepository;
            this.userRepository = userRepository;
            this.messageHandler = messageHandler;
        }

        //
        // GET: /Home/

        public ViewResult Index()
        {
            var homeIndexViewModel = new HomeIndexViewModel();

            try
            {
                homeIndexViewModel.Message = configuration.SiteTitle;

                //var currentUser = userRepository.GetUserByLoginID(User.Identity.Name);
                // Ensure we received a user object back and that user object is of type Admin
                //if (currentUser != null)
                //{
                    homeIndexViewModel.CurrentUserFullName = "Robert Williams";
                    //homeIndexViewModel.CurrentUserFullName = currentUser.FirstName + " " + currentUser.LastName;
                    //if (currentUser.IsAdmin)
                    //{
                    //    homeIndexViewModel.CurrentUserRole = UserRole.Administrator.GetStringValue();
                    //}
                    //else if (currentUser.IsAllocator)
                    //{
                    //    homeIndexViewModel.CurrentUserRole = UserRole.Owner.GetStringValue();
                    //}

                    //var allocationSummaryForUserViewModel = new AllocationSummaryForUserViewModel();

                    ////Get the Allocation Details for the current user
                    //allocationSummaryForUserViewModel.UserName = currentUser.Name;

                    //homeIndexViewModel.AllocationSummaryForUser = allocationSummaryForUserViewModel;
                //}

                return View("Index", homeIndexViewModel);
            }
            catch (Exception ex)
            {
                messageHandler.HandleError(ex);
                // If the currentUser object is not found then we need to provide a friendly error message 
                // for the end user and an error view to display that friendly error message.
                var errorViewModel = new ErrorViewModel();
                errorViewModel.FriendlyErrorMessage = "The following user is not an administrator of this application: " + User.Identity.Name;
                return View("Error", errorViewModel);
            }
        }

        public ActionResult Welcome()
        {
            return View();
        }

    }
}
